import { RpcCallRes, RpcHandler, RpcHandlerProxy } from "kilom";
import { exitGame, getPlayer, getRoom, getRoomIds, joinGame } from "../../../game/game";

export default class RpcGameMain extends RpcHandler {

    join(sid: string, name: string): RpcCallRes<{ uid: number, roomIds: number[] }> {
        return {
            data: {
                uid: joinGame(sid, name),
                roomIds: getRoomIds()
            }
        };
    }

    chat(uid: number, str: string): RpcCallRes {
        let player = getPlayer(uid);
        if (!player) {
            return { err: { code: 1, msg: "没找到玩家" } };
        }
        let room = getRoom(player.roomId);
        if (!room) {
            return { err: { code: 2, msg: "没找到房间" } };
        }
        room.chat(uid, str);
        return {};
    }

    leave(uid: number): RpcCallRes {
        exitGame(uid);
        return {};
    }

}

declare global {
    interface Rpc {
        game: {
            main: RpcHandlerProxy<RpcGameMain>
        }
    }
}